/**
 * https://leetcode.cn/problems/max-consecutive-ones-iii/submissions/560470246/
 * medium, 陈贝纯 2024.09.01
 * 1004. 最大连续1的个数 III
 * 滑动窗口
 */

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int sum=0;
        int l=0;
        int d=0;
        for(int r=0;r<nums.size();++r){
            if(nums[r]==0)
            k--;
            if(k>=0){
                while(r<nums.size()-1&&nums[r+1]==1)
                    r++;
                sum=max(sum,r-l+1);
            }
            if(k<=0){
                while(l<=r&&nums[l]==1)
                    l++;
                k++;
                l++;
            }
        }
        return sum;
    }
};